
var app = getApp()
import { randomString, randomTradeNo, getDate ,rTime,compareDateNow,countDownNum  } from '../../utils/util';
Page({
  data: {
    isLogin: false,
    showPhone: false,
    userInfo: {},
    gridIcon: [],
    time: 0,
    sex: 0,
  },

  toLogin(e) {
    const res = e.detail.userInfo
    // 调用云函数
    wx.cloud.callFunction({
      name: 'login',
      data: {
        $url: 'user',
        ava: res.avatarUrl,
        name: res.nickName,
        sex: res.gender
      },
      success: res => {
        console.log(res)
        if (res.result) {
          this.initUserData(res.result)
          wx.setStorageSync('isLogin', true)
          wx.showToast({
            title: '登录成功',
            icon: 'success',
            duration: 1000
          })
        }
      },
      fail: err => {
        console.error(err)
      }
    })
  },



  // toMember(){
  //   wx.navigateTo({
  //     url: '/pages/member/member',
  //   })
  // },






  onPay() {
    const nonceStr = randomString()
    const outTradeNo = randomTradeNo()
    const money = 1
    wx.cloud.callFunction({
      name: 'wxMakeOrder',
      data: {
        money: money,
        nonceStr,
        outTradeNo
      },
      success: (res) => {
        console.log(res)
        // 取得云函数返回的订单信息
        const payment = res.result.payment;
        // 调起微信客户端支付
        wx.requestPayment({
          ...payment,
          success:(res)=>{
            /* 支付成功回调 */
            console.log(res)
            this.ordernext()          
          },
          fail:(res)=> {
            /* 支付失败、未支付回调 */
            console.log(res)         
          }
        })
      }

    })
  },

  ordernext(){
    wx.cloud.callFunction({
      name: 'addMember',
      data: {
        $url: 'checkOrder'
      },
      success: (res) => {
        console.log(res)
        if(res.result.code==0){
          const order_type = res.result.data.order_type
          const create_time =res.result.data.order_start
          this.changeMember(create_time,order_type)
        }else{
         
        }
             
      }
    })
  },

  changeMember(time,type){
    let memberLen = 0 //会员七天
    switch (type) {
      case 1:
        memberLen = 7
        break;
      default:
        memberLen = 7
        break;
    }
    const start_time = rTime(time)
    console.log(start_time)
    const arr = start_time.split(' ')
    const end = getDate(start_time,7)
    const end_time = end.fullDate +' '+ arr[1]
    wx.cloud.callFunction({
      name: 'addMember',
      data: {
        $url:'add',
        start_time,
        end_time,
        member:'1',    
      },
      success: (res) => {
        console.log(res)
        if(res.result.code==0){
          this.reLogin()
        }
      }
    })
    
  },

  
  reLogin() {
    const openId = wx.getStorageSync('userInfo').openId
    wx.cloud.callFunction({
      name: 'login',
      data: {
        $url: 'freshLogin',
        openId: openId
      },
      success: res => {
        console.log(res)
        if (res.result) {
          const data = res.result.res.data[0]
          wx.setStorageSync('isLogin', true)
          this.initUserData(data)
        }
      },
      fail: err => {
        console.error(err)
      }
    })
  },

  initUserData(data) {
    app.globalData.isLogin = true
    app.globalData.userInfo = data
    app.globalData.member = data.member
    app.globalData.langNo = data.langNo
    this.onLoad()
  },

  //会员时间判断
  initMember(time){
    //判断结束时间 是否已过期
    const time2 = time.replace(/\-/g, "/")
    console.log(time2) //ios时间只认此格式 2021/5/10 09:55:03
    const isOver = compareDateNow(time2)
    console.log(isOver)
    if(isOver){
      //会员过期 更新数据库 将member改为0
      this.countEnd()
    }else{
      const counttime = countDownNum(time2)
      console.log(counttime)
      this.setData({
        time:counttime
      })
    }
  },

  //倒计时结束 更新数据库 将member改为0
  countEnd(){
    wx.cloud.callFunction({
      name: 'login',
      data: {
        $url: 'overTime'
      },
      success: res => {
        console.log(res)
        this.onLoad()
      }
    })
  },

  initPage() {
    //图标导航
    const gridIcon = [
      {
        url: '/image/video.png',
        text: '海量接单',
      }, {
        url: '/image/love.png',
        text: '喜好匹配',
      }, {
        url: '/image/1-1.png',
        text: '一对一客服',
      }
    ]
    this.setData({
      gridIcon
    })
  },

  onRadio(e) {
    console.log(e.detail)
    this.setData({
      sex: e.detail
    })
  },

  closePop() {
    wx.showModal({
      title: '提示',
      content: '确定不进行实名认证吗',
      success: res => {
        if (res.confirm) {
          this.setData({
            showPhone: false
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  getPhone(e) {
    this.setData({
      showPhone: false
    })
    console.log(e)
    const cloudID = e.detail.cloudID
    const openId = wx.getStorageSync('userInfo').openId
    console.log(openId)
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: 'login',
      data: {
        $url: 'phone',
        weRunData: wx.cloud.CloudID(cloudID), // 这个 CloudID 值到云函数端会被替换
        sex: this.data.sex,
        openId: openId
      }
    }).then(res => {
      console.log(res)

      wx.hideLoading()
      this.reLogin()
    })
  },

  //认证
  onDienty() {
    const phone = this.data.userInfo.phone
    if(phone){
      return
    }
    this.setData({
      showPhone: true
    })
  },
  //关于
  toAbout() {
    wx.navigateTo({
      url: '/pages/about/about',
    })
  },
  //在线客服
  toInline() {
    // console.log('123')
    // app.toast('开发中')
    wx.openCustomerServiceChat({
      extInfo: {url: 'https://work.weixin.qq.com/kfid/kfc757bb053f0a1e730'}, //客服链接
      corpId: 'ww08bc768cb9808002', //企业ID
      success(res) {
        console.log(res)
      }
    })
  },


  onLoad: function () {  
    wx.login()
    // const nowTime = new Date().getTime()
    this.initPage()
    const isLogin = app.globalData.isLogin
    const userInfo = app.globalData.userInfo
    if (isLogin) {
      this.setData({
        isLogin,
        userInfo,
      })
      if(userInfo.member!==0){
        //更新会员 倒计时
        this.initMember(userInfo.member_end)
      }
    } else {
      this.setData({ isLogin })
    }
  },

  onShow: function () {
    //每次进入页面刷新
    const userInfo = app.globalData.userInfo
    if(userInfo.member!==0){
      this.initMember(userInfo.member_end)
    }
  },


 
})
